package com.tools.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * List工具类
 */
public class ListUtils {

    /**
     * 根据目标容量 划分List
     * @param source
     * @param capacity 划分完成的单个List容量
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitListBycapacity(List<T> source, int capacity) {
        List<List<T>> result = new ArrayList<List<T>>();
        if (source != null) {
            int size = source.size();
            if (size > 0) {
                for (int i = 0; i < size; ) {
                    List<T> value = null;
                    int end = i + capacity;
                    if (end > size) {
                        end = size;
                    }
                    value = source.subList(i, end);
                    i = end;
                    result.add(value);
                }
            } else {
                result = null;
            }
        } else {
            result = null;
        }
        return result;
    }

}
